var tb;
$(function() {
	restoreDefault();
	$('#start').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		todayHighlight : true
	});
	$('#end').datetimepicker({
		format : 'yyyy-mm-dd',
		minView : 2,
		autoclose : true,
		endDate : new Date(),
		pickerPosition : 'bottom-left',
		todayHighlight : true
	});
});
function initData() {
	$.getJSON("/rest/fault/getFaultHisRebackByStartAndEnd/" + $('#start').val()
			+ "/" + $('#end').val(), function(result) {
		loadTable(result);
	});
}
function restoreDefault() {
	$('#start').val($('#startTime').val());
	$('#end').val($('#endTime').val());
	initData();
}
function query() {
	initData();
}
function loadTable(data) {
	if (tb) {
		tb.getTable().destroy();
	}
	var dataset = [];
	for ( var d in data) {
		var col = [];
		col[0] = Number(d) + 1;
		col[1] = getString(data[d]['subControlArea']);
		col[2] = getTime(data[d]['faultTime']);
		col[3] = getString(data[d]['faultEquiment']);
		col[4] = getString(data[d]['lightningGPS']);
		col[5] = getString(data[d]['linePatrolReasult']);
		dataset[d] = col;
	}

	var myOpt = MyTable.getDefaultOptions();
	myOpt.paging = true;
	myOpt.pageLength = 5;
	tb = new MyTable('datatable', [ '序号', '调管范围', '故障时间', '故障设备', '雷击定位系统记录',
			'巡线结果' ], myOpt);
	tb.setDataSet(dataset);
	tb.show();
}
function getTime(v) {
	if (!v) {
		return '';
	} else {
		v = new Date(v.time ? v.time : v).toLocaleString();
		return v;
	}
}
function getString(v) {
	return v ? v : '';
}
function resizeHtml() {
	if (tb) {
		tb.getTable().draw();
	}
}